<?php

if (!defined('TOBBIVM'))
	header('location:/template/notrepassing.php');
/**
 * This file is part of TobbiVMShop.
 *
 * TobbiVMShop is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TobbiVMShop is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TobbiVMShop.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * create article editor page
 * get: article.editor.tpl.php
 * used from article.page.php
 *
 * @version    $Id$
 * @package    TobbiVM-Shop
 * @copyright  Copyright (C) 2012
 * @author     Norbert Gebert
 * @license    GPL3
 */
db::getInstance()->where("product_id", $idx);
$result = db::getInstance()->getOne("product");

if ($result === NULL)
{
	message::setErrorRow(_('No data could be found! - Contact Admin!'), true);
}
else
{
	$functions = 'module/function/article.func.php';
	if (is_file($functions))
		include_once $functions;

	$refPage->assign('formStart', 'index2.php?page=article&sub=article&func=save');
	$refPage->assign('pageTitle', _('Article-Editor'));

	//-Tab-Menu------------------
	$refPage->assign('tab1', _('Description'));
	$refPage->assign('tab2', _('Options'));
	$refPage->assign('tab3', _('Images'));
	$refPage->assign('tab4', _('Display'));
	$refPage->assign('tab5', _('Meta-Tags'));
	$refPage->assign('tab6', _('Data'));

	//-1-------------------------
	$refPage->assign('id', _('Article-ID') . ': ');
	$refPage->assign('idx',
		'<input type="text" name="idxx" id="idxx" disabled="disabled" ' .
		'size="8" value="' . $result['product_id'] . '" />');

	$refPage->assign('sku', _('SKU') . ': ');
	$refPage->assign('skux',
		'<input type="text" name="skux" id="skux" size="64" ' .
		'value="' . $result['product_sku'] . '" />');
	$refPage->assign('articleSkuHelp', help('articleSku', $page));

	$refPage->assign('name', _('Productname') . ': ');
	$refPage->assign('namex',
		'<input type="text" name="namex" id="namex" size="64" ' .
		'value="' . $result['product_name'] . '" />');
	$refPage->assign('articleNameHelp', help('articleName', $page));

	// read and set categorynames
	$categoryy = '<select style="width:350px;" size="1" name="categoryx" id="categoryx">';
	$categorys = db::getInstance()->rawQuery("SELECT category_id, category_name, " .
		"category_active, category_order, category_top FROM category" .
		" WHERE category_top = '0' ORDER BY category_order");
	$categorys = sortCategorys($categorys);

	if ($result['product_category'] === '0')
	{
		$categoryy .= '<option value="0" selected="selected">' .
			_('Top-category') . '</option>';
	}
	else
	{
		$categoryy .= '<option value="0" >' . _('Top-category') . '</option>';
	}
	foreach ($categorys as $cat)
	{
		if ($cat['category_id'] === $result['product_category'])
		{
			$categoryy .= '<option value="' . $cat['category_id'] . '" selected="selected">' .
				$cat['category_name'] . '</option>';
		}
		else
		{
			$categoryy .= '<option value="' . $cat['category_id'] . '" >' .
				$cat['category_name'] . '</option>';
		}
	}
	$categoryy .= '</select>';
	$refPage->assign('category', _('Category') . ': ');
	$refPage->assign('categoryx', $categoryy);
	$refPage->assign('articleCategoryHelp', help('articleCategory', $page));

	// set activ-status
	$refPage->assign('active', _('Activ') . ': ');
	$activey = '<input type="checkbox" name="activex" id="activex" value="activex" ';

	if ($result['product_active'] === '0')
	{
		$activey .= '/>';
	}
	else
	{
		$activey .= 'checked />';
	}
	$refPage->assign('activex', $activey);
	$refPage->assign('articleActiveHelp', help('articleActive', $page));

	// read and set articledetail-browsepages  selected="selected"
	$browsepages = array();
	foreach (getBrowsepages('article') as $file)
	{
		if ($file === $result['product_browsepage'])
		{
			$browsepages[] = array('browsepage_nrx' => $file,
			 'browsepage_textx' => $file,
			 'checkedx' => 'selected="selected"');
		}
		else
		{
			$browsepages[] = array('browsepage_nrx' => $file,
			 'browsepage_textx' => $file,
			 'checkedx' => '');
		}
	}
	$refPage->assign('browsepages', $browsepages);
	$refPage->assign('browsepage', _('Browsepage') . ': ');
	$refPage->assign('articleBrowsepageHelp', help('articleBrowsepage', $page));

	$refPage->assign('shorttext', _('Short-description'));
	$refPage->assign('shorttextx',
		'<textarea style="visibility: hidden; display: none;" class="ckeditor"' .
		'name="shorttextx" id="shorttextx" rows="5" ' .
		'cols="56">' . $result['product_short_description'] . '</textarea>');
	$refPage->assign('shortDescriptionHelp', help('articleShortDescription', $page, TRUE));

	$refPage->assign('text', _('Description'));
	$refPage->assign('textx',
		'<textarea style="visibility: hidden; display: none;" class="ckeditor"' .
		'name="textx" id="textx" rows="25" ' .
		'cols="56">' . $result['product_description'] . '</textarea>');
	$refPage->assign('longDescriptionHelp', help('articleLongDescription', $page, TRUE));
	$refPage->assign('textHelp', help('articleText', $page, TRUE));

	//-2-Options-----------------
	// stock
	$refPage->assign('stocks', _('Stock-Quantity') . ': ');
	$refPage->assign('stocksx',
		'<input type="text" name="stocksx" id="stocksx" size="12" ' .
		'value="' . $result['product_stocks_quantity'] . '" />');
	$refPage->assign('articleStockHelp', help('articleStock', $page));

	// tax
	$tax_ids = db::getInstance()->rawQuery("SELECT tax_id, tax_value, tax_name FROM tax" .
		" ORDER BY tax_value");
	$taxy = array();
	foreach ($tax_ids as $tax_entry)
	{
		if ($tax_entry['tax_id'] === $result['product_tax_id'])
		{
			$taxy[] = array('taxValuex' => $tax_entry['tax_id'],
			 'taxNamex' => ($tax_entry['tax_value'] . '% - ' . $tax_entry['tax_name']),
			 'selectx' => 'selected="selected"');
		}
		else
		{
			$taxy[] = array('taxValuex' => $tax_entry['tax_id'],
			 'taxNamex' => ($tax_entry['tax_value'] . '% - ' . $tax_entry['tax_name']),
			 'selectx' => ' ');
		}
	}
	$refPage->assign('tax', _('Tax') . ': ');
	$refPage->assign('taxx', $taxy);
	$refPage->assign('articleTaxHelp', help('articleTax', $page));

	// price
	db::getInstance()->orderBy('price_name', 'ASC');
	db::getInstance()->where('price_art', 1, '<>');
	$cols = array('price_id', 'price_name');
	$price_ids = db::getInstance()->get('price', NULL, $cols);

	$pricey = array();
	foreach ($price_ids as $price_entry)
	{
		if ($price_entry['price_id'] === $result['product_price_id'])
		{
			$pricey[] = array('priceValuex' => $price_entry['price_id'],
			 'priceNamex' => $price_entry['price_name'],
			 'selectx' => 'selected="selected"');
		}
		else
		{
			$pricey[] = array('priceValuex' => $price_entry['price_id'],
			 'priceNamex' => $price_entry['price_name'],
			 'selectx' => ' "');
		}
	}
	$refPage->assign('price', _('Price-Id') . ': ');
	$refPage->assign('pricex', $pricey);
	$refPage->assign('articlePriceHelp', help('articlePrice', $page));

	// Display Fixprice
	db::getInstance()->where("price_id", $result['product_price_id']);
	$resultPrice = db::getInstance()->getOne("price");
	if ($resultPrice['price_art'] == 1)
		$refPage->assign('priceFixx',
			'<input type="text" oninput="setPricelist()" name="priceFixx" id="priceFixx" ' .
			'size="15" value="' . str_replace('.', ',', $resultPrice['price_per_item']) . '" />');
	else
		$refPage->assign('priceFixx',
			'<input type="text" name="priceFixx" id="priceFixx" ' .
			'size="15" value="' . str_replace('.', ',', 0.00) . '" />');

	$refPage->assign('priceFix', _('Price') . ': ');
	$refPage->assign('articlePriceFixHelp', help('articlePriceFix', $page));

	// read and set Userdefined attributelist selected="selected"
	$attributes = array();
	$attributeList = db::getInstance()->rawQuery("SELECT * FROM attribute " .
		"ORDER BY 'attribute_title' ASC");

	if ($result['product_attribute_id'] === '0' or
		$result['product_attribute_id'] == NULL)
	{
		$attributes[] = array('attribute_nrx' => '0',
		 'attribute_titelx' => _('No Attribute'),
		 'checkedx' => 'selected="selected"');
	}
	else
	{
		$attributes[] = array('attribute_nrx' => '0',
		 'attribute_titelx' => _('No Attribute'),
		 'checkedx' => '');
	}
	foreach ($attributeList as $attribute)
	{
		if ($attribute['attribute_id'] === $result['product_attribute_id'])
		{
			$attributes[] = array('attribute_nrx' => $attribute['attribute_id'],
			 'attribute_titelx' => $attribute['attribute_title'],
			 'checkedx' => 'selected="selected"');
		}
		else
		{
			$attributes[] = array('attribute_nrx' => $attribute['attribute_id'],
			 'attribute_titelx' => $attribute['attribute_title'],
			 'checkedx' => '');
		}
	}
	$refPage->assign('attributes', $attributes);
	$refPage->assign('attributpage', _('Attributepage') . ': ');
	$refPage->assign('articleAttributeHelp', help('articleAttribute', $page));

	//-3-Images-----------------
	$refPage->assign('image', _('Big image') . ': ');
	$refPage->assign('imageName', $result['product_image']);
	$refPage->assign('imageNote',
		_('Maximum') . ': ' . ARTICLE_IMAGE_SIZE . ' ' .
		_('Bytes'));
	$refPage->assign('imagex',
		'<input type="file" size="60" name="imagefile"' .
		'id="imagefile" />');
	$file = '../images/' . ARTICLE_IMAGE_PATH . '/' . $result['product_category'] . '/' .
		$result['product_image'];
	$refPage->assign('articleImageHelp', help('articleImage', $page));

	if (is_file($file))
	{
		$refPage->assign('imagey',
			'<img style="border:1px solid black;" src="' . '../images/' . ARTICLE_IMAGE_PATH . '/' .
			$result['product_category'] . '/' . $result['product_image'] . '">');
	}
	else
	{
		$refPage->assign('imagey', _('<h2>No picture available!</h2>'));
	}

	$refPage->assign('thumbImage', _('Thumb image') . ': ');
	$refPage->assign('thumbImageName', $result['product_small_image']);
	$refPage->assign('thumbImageNote',
		_('Maximum') . ': ' . ARTICLE_THUMB_WITH . 'px ' .
		_('width') . ' * ' . ARTICLE_THUMB_HEIGHT . 'px ' .
		_('height'));
	$refPage->assign('thumbImagex',
		'<input type="file" size="60" name="thumbImagefile" id="thumbImagefile" />');
	$file = '../images/' . ARTICLE_IMAGE_PATH . '/' . $result['product_category'] . '/' .
		ARTICLE_IMAGE_THUMB_PATH . '/' . $result['product_small_image'];
	$refPage->assign('articleThumbimageHelp', help('articleThumbimage', $page));

	if (is_file($file))
	{
		$refPage->assign('thumbImagey',
			'<img style="border:1px solid black;" src="' . '../images/' . ARTICLE_IMAGE_PATH . '/' .
			$result['product_category'] . '/' . ARTICLE_IMAGE_THUMB_PATH .
			'/' . $result['product_small_image'] . '">');
	}
	else
	{
		$refPage->assign('thumbImagey', _('<h2>No picture available!</h2>'));
	}

	//-4-Display----------------
	//-5-Meta...----------------
	$refPage->assign('metadescription', _('Metadescription'));
	$refPage->assign('metadescriptionx',
		'<input type="text"size = 64 name="metadescriptionx" ' .
		'id="metadescriptionx" onmouseover="descriptionN(this.value)" ' .
		'onkeyup="descriptionN(this.value) "value="' .
		$result['product_metadescription'] . '" />');
	$refPage->assign('articleMetadescriptionHelp', help('articleMetadescription', $page));

	$refPage->assign('metakey', _('Metakeys'));
	$refPage->assign('metakeyx',
		'<input type="text"size = 64 name="metakeyx" ' .
		'id="metakeyx" onmouseover="keywordN(this.value)" ' .
		'onkeyup="keywordN(this.value)" value="' .
		$result['product_metakey'] . '" />');
	$refPage->assign('articleMetakeyHelp', help('articleMetakey', $page));

	//-6-Data-------------------
	$refPage->assign('sales', _('Sales') . ': ');
	$refPage->assign('salesx',
		'<input type="text" name="salesx" id="salesx" ' .
		'disabled="disabled" value="' . $result['product_sales'] . '" />');
	$refPage->assign('create', _('Created') . ': ');
	$refPage->assign('createx',
		'<input type="text" name="createx" id="createx" ' .
		'disabled="disabled" value="' .
		date('H:i:s - d.m.Y', strtotime($result['product_create'])) . '" />');

	$refPage->assign('lastChange', _('Last change') . ': ');
	$refPage->assign('lastChangex',
		'<input type="text" name="lastchangex" id="lastchangex" ' .
		'disabled="disabled" value="' .
		date('H:i:s - d.m.Y', strtotime($result['product_last_change'])) . '" />');

	//-Buttons------------------
	$refPage->assign('btnSave',
		'<input type="hidden" id="idx" name="idx" value="' . $idx . '">' .
		'<input type="hidden" id="oldCategory" name="oldCategory" value="' .
		$result['product_category'] . '">' .
		'<input type="hidden" name="oldImagex" value="' .
		$result['product_image'] . '">' .
		'<input type="hidden" name="oldSmallImagex" value="' .
		$result['product_small_image'] . '">' .
		'<input type="hidden" id="orderx" name="orderx" value="' .
		$result['product_order'] . '">' .
		'<input type="hidden" id="priceIdy" name="priceIdy" value="' .
		$resultPrice['price_id'] . '">' .
		'<input type="submit" name="submit" id="save" value="' .
		_('Save') . '" />');

	$refPage->assign('btnRefresh',
		'<input type="submit" name="submit" id="refresh" value="' .
		_('Refresh') . '" />');

	$refPage->assign('btnAbort',
		'<input type="submit" name="submit" id="abort" value="' .
		_('Cancel') . '" />');

	$refPage->addtpl('content', '/' . $page . '/' . $subpage . '.editor.tpl.php');
	$refPage->assign('messagebox', '');

	// Keyword- and descriptioncounter
	$refPage->assign('descriptionNumber', '<span id="descriptionNumber">0</span>');
	$refPage->assign('keywordNumber', '<span id="keywordNumber">0</span>');

	$metascript = '<script type="text/javascript">
		function descriptionN(text) 
		{
			var html = text.length;
			html = html + "   ' . _('Character') . ' (' . METADESCRIPTION_LENGH . ')";
			document.getElementById(\'descriptionNumber\').textContent = html;
		}
		function keywordN(text) 
		{
			var html = text.split(\',\').length;
			html = html + "   ' . _('Words') . ' (' . METAKEYWORD_NUMBER . ')";
			document.getElementById(\'keywordNumber\').textContent = html;
		}
	</script>';
	$refPage->assign('scripts',
		'<script type="text/javascript" src="' .
		'module/ckeditor/ckeditor.js"></script>' . $metascript);
}
?>